***** Replication File for "Going Nativist: Going Nativist: How Nativism and Economic Ideology Interact to Shape Beliefs about Global Trade"
***** This file contains code to replicate variable coding and appendix summary data; run before replicating analyses if starting with raw data
***** 17 November 2020
***** This file already contains factor scores for beliefs about globalization, generated in MPlus (see PRS_Table1 for implementation)

log using "C:\Users\f002mrh\Dropbox\Trade Attitudes\Data and Code\Replication Files for FPA\\UScodinglog.txt" 

**** Demographic characteristics, Partisanship, Ideology
*Education
tab educ, gen(educ_)
gen nocoll = educ_1 + educ_2
gen somecoll = educ_3 
gen twocoll = educ_4 
gen coll = educ_5
gen postgr = educ_6

gen university = 0
replace university = 1 if coll==1
replace university = 1 if postgr==1

*Race
gen white = 0
replace white = 1 if race==1
tab race, gen(race_)

*Gender
recode gender (1=1) (2=0), gen(male)

*Age
gen age1 = 0
replace age1 = 1 if age < 30
gen age2 = 0
replace age2 = 1 if age < 45 & age > 29
gen age3 = 0
replace age3 = 1 if age > 44 & age < 65
gen age4 = 0
replace age4 = 1 if age > 64

*Partisanship
gen repub = .
replace repub = 0 if w2USparty1==2 | w2USparty2==1
replace repub = 0 if w2USparty4==2 | w2USparty4==3 | w2USparty4==4
replace repub = 1 if w2USparty1==1 |w2USparty2==2
replace repub = 1 if w2USparty4==1

gen ind = 0
replace ind = 1 if w2USparty4==3 | w2USparty4==4

*Ideology
*coded such that higher values are more conservative
tab ideo5
gen ideodk = ideo5
recode ideodk (6=3)

*left/right partisan
tab w2wing1a

*Employment status
tab employ, gen(employ_)
gen workfull = 0
replace workfull = 1 if employ_1==1
gen workpart = 0
replace workpart = 1 if employ_2==1
gen workunemp = 0 
replace workunemp = 1 if employ_3==1 | employ_4==1 
gen workstudent = 0 
replace workstudent = 1 if employ_8==1
gen workretired = 0
replace workretired = 1 if employ_5==1
gen workother = 0
replace workother = 1 if employ_9==1 | employ_7==1 | employ_6==1
label var workother "Homemaker/permanently disabled/Other"

*Income, different measure from other version of data
tab faminc, gen(inc_)
*crosstab ecluding prefer not to say
tab faminc if faminc !=97 
*quartiles
gen inc30 = 0
replace inc30 = 1 if inc_1==1 | inc_2==1 |inc_3==1
gen inc3050 = 0
replace inc3050 = 1 if inc_4==1 | inc_5==1
gen inc5080 = 0
replace inc5080 = 1 if inc_6==1 | inc_7==1 | inc_8==1
gen inc80up = 0
replace inc80up = 1 if inc_9==1 | inc_10==1 | inc_11==1 | inc_12==1 |inc_17==1 | inc_13==1 | inc_14==1 | inc_15==1 | inc_16==1


*code don't know and prefer not to answer as missing
foreach var of varlist inc30 inc3050 inc5080 inc80up {
replace `var' = . if inc_18==1 
}


****Appendix Section 1.1: Nativism and Economic Perceptions Measurement 
*Percent of sample dk on each item - range from 4.73 to 10.18% in the U.S. sample
* DK = 7.11%
tab w2eupan1 
* DK = 4.73%
tab w2eupan2
*DK = 5.45%
tab w2eupan3
* DK = 5.24%
tab w2eupan4 
*DK = 6.05%
tab w2eupan5
*DK = 8.08%
tab w2eupan6
*DK = 10.18%
tab w2eupan7
*DK = 5.49%
tab w2eupan8

*Recode DK to midpoint*
recode w2eupan1 (6=3), gen(w2eupan1a)
recode w2eupan2 (6=3), gen(w2eupan2a)
recode w2eupan3 (6=3), gen(w2eupan3a)
recode w2eupan4 (6=3), gen(w2eupan4a)
recode w2eupan5 (6=3), gen(w2eupan5a)
recode w2eupan6 (6=3), gen(w2eupan6a)
recode w2eupan7 (6=3), gen(w2eupan7a)
recode w2eupan8 (6=3), gen(w2eupan8a)

*alpha = 0.8521
alpha w2eupan1a w2eupan2a w2eupan3a w2eupan4a w2eupan5a w2eupan6a w2eupan7a w2eupan8a, item

*Reverse code so Nativist response is high end of scale
recode w2eupan1a w2eupan3a w2eupan6a w2eupan8a (1=5) (2=4) (3=3) (4=2) (5=1)

*An additive scale
gen natism = w2eupan1a + w2eupan2a + w2eupan3a + w2eupan4a + w2eupan5a + w2eupan6a + w2eupan7a + w2eupan8a 

*Alpha for 2-item version = 0.54
alpha w2eupan1a w2eupan2a

*Belief in free market, market regulation (measured in previous wave)
tab w1fremkt1
tab w1regmkt1

foreach var of varlist w1fremkt1 w1regmkt1 {
gen `var'r=`var'
recode `var'r (6=3) (1=5) (2=4) (3=3) (4=2) (5=1)
}


*Additional Control Variables: 
*Economic Sentiment
*Retrospective economic sentiment
foreach var of varlist w2econ1 w2econ2 {
gen new`var'=`var'
recode new`var' (1=5) (2=4) (3=3) (4=2) (5=1) (6=3)
}


*Prospective economic sentiment 
foreach var of varlist w2econp1 w2econp2 {
gen new`var'=`var'
recode new`var' (6=3)
**(1=5) (2=4) (3=3) (4=2) (5=1) 
}

*These form a single factor
factor w2econ1 w2econ2 w2econp1 w2econp2, ml

*alpha = 0.85
alpha neww2econ1 neww2econ2 neww2econp1 neww2econp2, item gen(econsentimentw2)
*rescale 0 to 1
replace econsentimentw2=econsentimentw2-1
replace econsentimentw2=econsentimentw2/4


*Universalism
tab w2Univ2f1
recode w2Univ2f1 (7=3.5), gen(Univ2f)
tab w2Univ3f1
recode w2Univ3f1 (7=3.5), gen(Univ3f)
tab w2Univ2m1
recode w2Univ2m1 (7=3.5), gen(Univ2m)
tab w2Univ3m1
recode w2Univ3m1 (7=3.5), gen(Univ3m)
*recode so high end of the scale = more commitment to universalism
recode Univ2f Univ3f Univ2m Univ3m (1=6) (2=5) (3=4) (4=3) (5=2) (6=1)

*combine male and female responses into single variable
gen Univ2 = Univ2f 
replace Univ2 = Univ2m if Univ2m !=.
gen Univ3 = Univ3f
replace Univ3 = Univ3m if Univ3m !=.

gen univ = Univ2 + Univ3
*alpha = 0.7242
alpha Univ2 Univ3

*Repeat for conformity 
tab w2Conform1f1
recode w2Conform1f1 (7=3.5), gen(Conform1f)
tab w2Conform1m1
recode w2Conform1m1 (7=3.5), gen(Conform1m)
recode Conform1f Conform1m (1=6) (2=5) (3=4) (4=3) (5=2) (6=1)

*combine male and female responses into single variable
gen conform = Conform1f
replace conform = Conform1m if Conform1m !=.



**********************************
**** Coding for experimental data
*********************************

*Dependent Variable*
*Recode DK to midpoint*
recode w2trade3a (6=3), gen(tradea)
recode w2trade3b (6=3), gen(tradeb)

*Generate a variable for experimental condition*
gen tradeinfohi = .
replace tradeinfohi = 1 if tradea != .
replace tradeinfohi = 0 if tradeb != .

gen supptrade = .
replace supptrade = tradea if tradea != .
replace supptrade = tradeb if tradeb != .

log close

*Save the data to your directory
saveold "(directoryinformation)\PRS_replicationdata_UScoded.dta"

